var express = require("express");
var router = express.Router();
var bug = require("../service/bug/index");
let jwt = require("jsonwebtoken");
let userService = require("../service/userService");
//引入一个文件夹  默认就是引入该文件夹中的index文件

/* GET home page. */
router.post("/add", function (req, res, next) {
  //接收前端传来的数据  写入数据库  返回值

  let token = req.headers.token;
  jwt.verify(token, "abc", async (err, info) => {
    console.log(info);
    if (err) {
      res.json({
        code: 0,
        msg: "token解析异常，请重新登录",
      });
    } else {
      if (info.type == 2) {
        var { bugreason, start, end, DeveloperName ,usertype,imgurl} = req.body;
        bug
          .add({
            username: info.username,
            bugreason,
            start,
            end,
            type: 1,
            DeveloperName,
            usertype,
            imgurl
          })
          .then((data) => {
            if (data.length) {
              res.json({
                code: 1,
                msg: data
              });
            } else {
              res.json({
                code: 0,
                msg: "新增失败",
              });
            }
          });
      } else {
        res.json({
          code: 0,
          msg: "用户无权限进行此操作",
        });
      }
    }
  });

  // var { username, bugreason, start, end } = req.body;

  // bug.add({ username, bugreason, start, end, type: 1 }).then((data) => {
  //   if (data.length) {
  //     res.json({
  //       code: 1,
  //       msg: "新增成功",
  //     });
  //   } else {
  //     res.json({
  //       code: 0,
  //       msg: "新增失败",
  //     });
  //   }
  // });
});

// router.post("/query", function (req, res, next) {
//   //接收前端传来的数据  写入数据库  返回值
//   let token = req.headers.token;
//   jwt.verify(token, "abc", async (err, info) => {
//     console.log(info);
//     if (err) {
//       res.json({
//         code: 0,
//         msg: "token解析异常，请重新登录",
//       });
//     } else {
//       if (info.type == 2 || 3) {
//         // let { username } = req.body;
//         console.log(info.username);
//         let list = await bug.query({ username: info.username });
//         res.json({
//           code: 1,
//           msg: "获取",
//           data: list,
//         });
//       } else {
//         res.json({
//           code: 0,
//           msg: "用户无权限进行此操作",
//         });
//       }
//     }
//   });
// });

//分页------------------------------------------------------------
router.get("/query", function (req, res, next) {
  //接收前端传来的数据  写入数据库  返回值
  let token = req.headers.token;
  jwt.verify(token, "abc", async (err, info) => {
    if (err) {
      res.json({
        code: 0,
        msg: "token解析异常，请重新登录",
      });
    } else {
      if (info.type == 2 || 3) {
        let { page, shownum, key, type } = req.query; //接受参数 get请求用query
        let params = {};
        params.username=info.username
        let skipnum = (page-1)*shownum;
        let reg = new RegExp(key);
        params.bugreason = reg; //精确查询
        if (type) {
          params.type = type * 1;
        }
      
        let count = await bug.count(params);//获取数据总数
        let list = await bug.query(params,skipnum * 1, shownum * 1);
        res.json({
          code: 1,
          msg: "获取",
          data: list,
          count:count
        });
      } else {
        res.json({
          code: 0,
          msg: "用户无权限进行此操作",
        });
      }
    }
  });
});

//获取开发人员
router.get("/userlist", (req, res, next) => {
  let token = req.headers.token;
  jwt.verify(token, "abc", async (err, info) => {
    if (err) {
      res.json({
        code: 0,
        msg: "token解析异常，请重新登录",
      });
    } else {
      if (info.type == 2) {
        let list = await userService.query();
        if (list.length) {
          res.json({
            code: 1,
            msg: "查询成功",
            list,
          });
        } else {
          res.json({
            code: 0,
            msg: "查询数据为空",
          });
        }
      } else {
        res.json({
          code: 0,
          msg: "查询用户权限不够",
        });
      }
    }
  });
});


//更改BUG状态
router.patch("/update", (req, res, next) => {
  
  let { _id, type } = req.body;
  let token = req.headers.token;
  jwt.verify(token, "abc", async function (err, code) {
    if (!err) {
      if (code.type == 2) {
        let data = await bug.update({_id}, { $set: { type } });
        res.json({
          code: 1,
          msg: data,
        });
      } else {
        res.json({
          code: 0,
          msg: "权限不够",
        });
      }
    } else {
      res.json({
        code: 0,
        msg: "未登录" + err,
      });
    }
  });
});


//删除BUG
router.delete("/remove", (req, res, next) => {
  let { _id} = req.body;
  let token = req.headers.token;
  jwt.verify(token, "abc", async function (err, code) {
    if (!err) {
      if (code.type == 2) {
        let data = await bug.remove({_id});
        res.json({
          code: 1,
          msg: data,
        });
      } else {
        res.json({
          code: 0,
          msg: "权限不够",
        });
      }
    } else {
      res.json({
        code: 0,
        msg: "未登录" + err,
      });
    }
  });
});


module.exports = router;
